package utils;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 文件通用-工具类
 */
public class Util {

    // 判断一个对象，是否为空
    public synchronized static boolean isEmpty(Object d){
        return d==null||d.toString().trim().toLowerCase().equals("null")||d.toString().trim().equals("");
    }

    /** 获取路径下的，文件后缀，大写转换 */
    public static String getWjHz(String fileName){
        if (StringUtils.isBlank(fileName)){
            return "";
        }

        String fileTyle = "";
        try {
            fileTyle = fileName.substring(fileName.lastIndexOf("."),fileName.length());
        }catch (Exception e){
            return fileName;
        }

        return fileTyle.toLowerCase();
    }

    // 根据带“/”的路径获取带后缀文件名
    public static String getWjmc(String fileName){
        if (StringUtils.isBlank(fileName)){
            return "";
        }
        String fileTyle = fileName.substring(fileName.lastIndexOf("/")+1,fileName.length());
        return fileTyle;

    }

    // 根据全路径获取最后一个“/”之前的路径名称
    public static String getWjjmc(String fileName){
        if (StringUtils.isBlank(fileName)){
            return "";
        }
        String fileTyle = fileName.substring(0,fileName.lastIndexOf("/"));
        return fileTyle;

    }

    // 根据带后缀文件名，获取不带后缀文件名
    public static String getWjmcNoHz(String fileName){
        try {
            return fileName.substring(0,fileName.lastIndexOf("."));
        }catch (Exception e){
            return fileName;
        }
    }

    // 根据文件后缀，返回不同的文件下载类型
    public static String getContenTypeByWjhz(String wjhz){
        if(".xls".equals(wjhz)){
            return "application/vnd.ms-excel";
        }
        if(".xlsx".equals(wjhz)){
            return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        }
        if (".docx".equals(wjhz)){
            return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        }
        if (".doc".equals(wjhz)){
            // application/msword (word文档格式)
            return "application/msword";
        }
        if (".pdf".equals(wjhz)){
            return "application/pdf";
        }
        if (".jpg".equals(wjhz) || ".jpeg".equals(wjhz)){
            return "image/jpeg";
        }
        if (".png".equals(wjhz)){
            return "image/png";
        }
        if (".gif".equals(wjhz)){
            return "image/gif";
        }
        if (".zip".equals(wjhz)){
            return "application/zip";
        }
        if (".tar".equals(wjhz)){
            return "application/x-tar";
        }
        if (".gz".equals(wjhz)){
            return "application/gzip";
        }
        if (".txt".equals(wjhz)){
            return "text/plain; charset=utf-8";
        }
        if (".md".equals(wjhz)){
            return "text/markdown";
        }
        if (".ppt".equals(wjhz)){
            return "application/vnd.ms-powerpoint";
        }
        if (".pptx".equals(wjhz)){
            return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
        }
        if (".xml".equals(wjhz)){
            return "application/xml; charset=utf-8";
        }
        if (".mp3".equals(wjhz)){
            return "audio/mpeg";
        }
        if (".mp4".equals(wjhz)){
            return "video/mp4";
        }
        if (".rp".equals(wjhz)){
            return "application/x-rp";
        }
        if (".wps".equals(wjhz)){
            return "application/vnd.ms-works";
        }
        if (".html".equals(wjhz) || ".htm".equals(wjhz)){
            return "text/html; charset=utf-8";
        }
        if (".apk".equals(wjhz)){
            return "application/octet-stream";
        }
        if (".bmp".equals(wjhz)){
            return "image/bmp";
        }
        if (".tiff".equals(wjhz)){
            return "image/tiff";
        }
        if (".webp".equals(wjhz)){
            return "image/webp";
        }
        if (".svg".equals(wjhz)){
            return "image/svg+xml";
        }

        return "";
    }

    /**
     * 根据文件后缀验证是否为图片
     * */
    public static boolean verifyIsImageByWjhz(String wjhz){
        List<String> imageList = CollectionUtil.newArrayList(
                ".jpg",
                ".jpeg",
                ".png",
                ".gif",
                ".bmp",
                ".tiff",
                ".webp",
                ".svg"
        );

        return imageList.contains(wjhz);
    }

    /**
     * 获取.后面的内容,转小写
     * */
    public static String getExtension(String fileName) {
        if (StringUtils.isBlank(fileName)){
            return "";
        }

        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) {
            // 没有找到点，返回空字符串或者null（根据你的需要）
            return fileName;
        }

        // 返回扩展名（包括点号后面的部分）
        return fileName.substring(dotIndex + 1).toLowerCase();
    }
}
